package com.learn.springcloud.dubbo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.learn.springcloud.dubbo.api.service.TestService;
import com.learn.springcloud.dubbo.config.Test;
import com.learn.springcloud.dubbo.entity.TestEntity;
import com.learn.springcloud.dubbo.mapper.TestMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;

/**
 * @Author: 胡伟
 * @Date: 2020/11/19 16:06
 * @Description:
 */
@DubboService
public class TestServiceImpl implements TestService {
    @Resource
    private Test test;
    @Resource
    private TestMapper testMapper;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public String sayHello() {
        return test.getName();
    }

    @Override
    public String getName() {
        TestEntity testEntity = testMapper.selectOne(new LambdaQueryWrapper<TestEntity>().eq(TestEntity::getId, "1"));
        return testEntity.getName();
    }

    @Override
    public String getRedissionCache(String key) {
       RBucket<String> bucket =  redissonClient.getBucket(key,new StringCodec());
       redissonClient.getBucket("test").set("123456");
       return bucket.get();
    }

    @Override
    public String getJedisCache(String key) {
        return redisTemplate.opsForValue().get(key);
    }
}
